package cn.yr.blog.controller.web;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ObjectUtil;
import cn.yr.blog.service.CommentService;
import cn.yr.common.core.domain.entity.SysUser;
import cn.yr.common.core.domain.entity.YrComment;
import cn.yr.common.core.domain.vo.PageVo;
import cn.yr.common.core.manager.AuthUserManager;
import cn.yr.common.exception.ret.Ret;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 评论
 *
 * @author Lang
 * @email 1102076808@qq.com
 * @date 2020-07-30 21:23:23
 */

@Api(tags = "web评论")
@RestController
@RequestMapping("/web/comment")
public class WebCommentController {

	@Resource
	private AuthUserManager authUserManager;

	@Autowired
	private CommentService commentService;

	/**
	 * 分页列表
	 */
	@RequestMapping("/page")
	public Ret page(@RequestBody PageVo<YrComment> pageVo) {
		PageVo<YrComment> pageYrComment = commentService.page(pageVo, new QueryWrapper<>());
		return Ret.success(pageYrComment);
	}

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public Ret list() {
		return Ret.success(commentService.list());
	}


	/**
	 * @description
	 * @author Lang
	 * @date 2020/7/30 21:35
	 */
	@RequestMapping("/listTreeArticleComment/{articleId}")
		public Ret listTreeArticleComment(@PathVariable("articleId") String articleId) {
		QueryWrapper<YrComment> wrapper = new QueryWrapper<>();
		wrapper.orderByDesc("create_time");
		wrapper.eq(ObjectUtil.isNotEmpty(articleId),"article_id",articleId);
		/** 只显示审核通过的 */
		wrapper.eq("is_review","1");
		List<YrComment> list = commentService.list(wrapper);
		List<Tree<Integer>> trees = commentService.buildCommentTree(list);
		return Ret.success(trees);
	}



	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	public Ret info(@PathVariable("id") Integer id) {
		YrComment yrComment = commentService.getById(id);
		return Ret.success(yrComment);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/add")
	public Ret save(@Validated @RequestBody YrComment yrComment) {
		yrComment.setCreateTime(new Date());
		SysUser user = authUserManager.getUser().getSysUser();
		yrComment.setSender(authUserManager.uid().intValue());
		yrComment.setSenderName(user.getUserName());
		yrComment.setSenderAvatar(user.getAvatar());
		yrComment.setSenderOauth(user.getOauthType());
		commentService.save(yrComment);
		return Ret.success();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/edit")
	public Ret update(@Validated @RequestBody YrComment yrComment) {
		commentService.updateById(yrComment);
		return Ret.success();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/remove")
	public Ret delete(@RequestBody Integer[] ids) {
		commentService.removeByIds(Arrays.asList(ids));
		return Ret.success();
	}

}
